package com.cryptoneedle.garden.service.crud.garden.source;

import com.cryptoneedle.garden.entity.source.metadata.SourceCatalog;
import com.cryptoneedle.garden.entity.source.metadata.SourceDatabase;
import com.cryptoneedle.garden.entity.source.metadata.SourceTable;
import com.cryptoneedle.garden.repository.source.constraint.*;
import com.cryptoneedle.garden.repository.source.enhance.SourceDimensionRepository;
import com.cryptoneedle.garden.repository.source.metadata.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>description:  </p>
 *
 * @author CryptoNeedle
 * @date 2025-10-23
 */
@Slf4j
@Service
@Transactional
public class PatchSourceService {

    @Autowired
    private FoundSourceService select;
    @Autowired
    private AddSourceService add;
    @Autowired
    private PatchSourceService patch;
    @Autowired
    private SaveSourceService save;
    @Autowired
    private DeleteSourceService delete;

    @Resource
    private SourceServerRepository sourceServerRepository;
    @Resource
    private SourceCatalogRepository sourceCatalogRepository;
    @Resource
    private SourceDatabaseRepository sourceDatabaseRepository;
    @Resource
    private SourceTableRepository sourceTableRepository;
    @Resource
    private SourceColumnRepository sourceColumnRepository;

    @Resource
    private SourceConstraintCheckRepository sourceConstraintCheckRepository;
    @Resource
    private SourceConstraintForeignRepository sourceConstraintForeignRepository;
    @Resource
    private SourceConstraintPrimaryRepository sourceConstraintPrimaryRepository;
    @Resource
    private SourceConstraintUniqueRepository sourceConstraintUniqueRepository;
    @Resource
    private SourceIndexUniqueRepository sourceIndexUniqueRepository;

    @Resource
    private SourceDimensionRepository sourceDimensionRepository;

    /**
     * SourceServerRepository
     */

    /**
     * SourceCatalogRepository
     */
    public void catalogEnabled(String catalogName, boolean enabled) {
        SourceCatalog catalog = select.catalog(catalogName);
        if (catalog.getEnabled() != enabled) {
            catalog.setEnabled(enabled);
            save.save(catalog);
        }
    }

    public void catalogDefaultSystemCode(String catalogName, String defaultSystemCode) {
        SourceCatalog catalog = select.catalog(catalogName);
        String oldSystemCode = catalog.getDefaultSystemCode();

        if (!Strings.CS.equals(oldSystemCode, defaultSystemCode)) {
            catalog.setDefaultSystemCode(defaultSystemCode);
            save.save(catalog);

            List<SourceDatabase> databases = select.databases(catalog);
            for (SourceDatabase database : databases) {
                if (Strings.CS.equals(oldSystemCode, database.getDefaultSystemCode())) {
                    patch.databaseDefaultSystemCode(catalogName, database.getDatabaseName(), defaultSystemCode);
                }
            }
        }
    }

    /**
     * SourceDatabaseRepository
     */
    public void databaseEnabled(String catalogName, String databaseName, boolean enabled) {
        SourceDatabase database = select.database(catalogName, databaseName);
        if (database.getEnabled() != enabled) {
            database.setEnabled(enabled);
            sourceDatabaseRepository.save(database);
        }
    }

    public void databaseDefaultSystemCode(String catalogName, String databaseName, String defaultSystemCode) {
        SourceDatabase database = select.database(catalogName, databaseName);
        String oldSystemCode = database.getDefaultSystemCode();

        if (!Strings.CS.equals(oldSystemCode, defaultSystemCode)) {
            database.setDefaultSystemCode(defaultSystemCode);
            save.save(database);

            List<SourceTable> tables = select.tables(catalogName, databaseName);
            for (SourceTable table : tables) {
                if (Strings.CS.equals(oldSystemCode, table.getSystemCode())) {
                    patch.tableSystemCode(catalogName, database.getDatabaseName(), table.getTableName(), defaultSystemCode);
                }
            }
        }
    }

    /**
     * SourceTableRepository
     */
    public void tableEnabled(String catalogName, String databaseName, String tableName, boolean enabled) {
        SourceTable table = select.table(catalogName, databaseName, tableName);
        if (table.getEnabled() != enabled) {
            table.setEnabled(enabled);
            sourceTableRepository.save(table);
        }
    }

    public void tableSystemCode(String catalogName, String databaseName, String tableName, String systemCode) {
        SourceTable table = select.table(catalogName, databaseName, tableName);
        if (!Strings.CS.equals(table.getSystemCode(), systemCode)) {
            table.setSystemCode(systemCode);
            save.save(table);
        }
    }

    /**
     * SourceColumnRepository
     */

    /**
     * SourceConstraintCheckRepository
     */

    /**
     * SourceConstraintForeignRepository
     */

    /**
     * SourceConstraintPrimaryRepository
     */

    /**
     * SourceConstraintUniqueRepository
     */

    /**
     * SourceIndexUniqueRepository
     */

    /**
     * SourceDimensionRepository
     */
}